package com.usian.controller;

import com.usian.pojo.Dept;
import com.usian.pojo.Emp;
import com.usian.pojo.EmpVo;
import com.usian.pojo.Zhiwu;
import com.usian.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@RequestMapping("emp")
@Controller
public class EmpController {
    @Autowired
    private EmpService empService;
    @RequestMapping("findAll")
    public String findAll(Model model,Integer type,Integer did){
        List<EmpVo> empList=empService.findAll(type,did);
        List<Dept> dList = empService.findDid();
        List<Zhiwu> zList = empService.findZid();
        model.addAttribute("empList",empList);
        model.addAttribute("type",type);
        model.addAttribute("dList",dList);
        model.addAttribute("zList",zList);
        return "list";
    }

    @RequestMapping("add")
    public String add(Emp emp){
        if (emp.getId()!=null){
            empService.edit(emp);
        }else {
            empService.add(emp);
        }
        return "redirect:/emp/findAll?type="+emp.getType();
    }
    @RequestMapping("toAdd{type}/{type}")
    public String toAdd(Model model,@PathVariable  Integer type){
        List<Zhiwu> zList=empService.findZid();
        List<Dept> dList=empService.findDid();
        model.addAttribute("zList",zList);
        model.addAttribute("dList",dList);
        model.addAttribute("type",type);
        return "add";
    }
    @RequestMapping("addDept")
    public String addDept(Dept dept){
        empService.addDept(dept);
        return "redirect:/emp/findAll?type="+dept.getType();
    }
    @RequestMapping("toAddDept{type}/{type}")
    public String toAddDept(Model model,@PathVariable  Integer type){
        model.addAttribute("type",type);
        return "addDept";
    }
    @RequestMapping("addZhiWu")
    public String addZhiWu(Zhiwu zhiwu){
        empService.addZhiWu(zhiwu);
        return "redirect:/emp/findAll?type="+zhiwu.getType();
    }
    @RequestMapping("toAddZhiWu{type}/{type}")
    public String toAddZhiWu(Model model,@PathVariable  Integer type){
        List<Dept> dList = empService.findDid();
        model.addAttribute("type",type);
        model.addAttribute("dList",dList);
        return "addZhiWu";
    }
    @RequestMapping("toEdit/{id}/{type}")
    public String toEdit(@PathVariable Integer id,Model model,@PathVariable Integer type){
        Emp emp=empService.findById(id);
        List<Zhiwu> zList=empService.findZid();
        List<Dept> dList=empService.findDid();
        model.addAttribute("zList",zList);
        model.addAttribute("dList",dList);
        model.addAttribute("emp",emp);
        model.addAttribute("type",type);
        return "edit";
    }
    @RequestMapping("delete/{id}/{type}")
    public String delete(@PathVariable Integer id,@PathVariable Integer type){
        empService.delete(id);
        return "redirect:/emp/findAll?type="+type;
    }
    @RequestMapping("deleteDept/{did}/{type}")
    public String deleteDept(@PathVariable Integer did,@PathVariable Integer type){
        empService.deleteDept(did);
        return "redirect:/emp/findAll?type="+type;
    }


}
